local AbsActExcel = require("excel.absAct")
local Gm = require("chat.Gm")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Log = require("common.Log")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ItemDefine = require("bag.ItemDefine")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local HeroGrid = require("hero.HeroGrid")
local DrawCardExcel = require("excel.drawCard").drawCard
local HeroExcel = require("excel.hero").hero
local MailExcel = require("excel.mail")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local HeroBook = require("hero.HeroBook")
local AbsActLogic = require("absAct.AbsActLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local ZhanbuLogic = require("zhanbu.ZhanbuLogic")
local MailManager = require("mail.MailManager")
local MailDefine = require("mail.MailIdDefine")
local LimitPerpleHeroLogic = require("present.LimitPerpleHeroLogic")
local CombatLogic = require("combat.CombatLogic")
local Grid = require("bag.Grid")
--[[
    absAct.xlxs-heroCome
    新英雄来袭-英雄降临
        1.可设置心仪英雄,若只有一个不需要设置,默认设置为第一个
        2.召唤英雄,1次或10次,统一从DrawCardLogic.draw调用
        3.登陆时检查活动结束回收道具
    DB:
        human.db.absAct[id] = {
            perpleHero = xxx,       -- 心仪英雄
            noTargetHeroCnt = xxx,  -- 距离必得英雄已完成的数量
            drawCardCnt = xxx,      -- 召换数量
        }        
    local:
    public:
        getAndSendMsg()             -- 发送活动数据
        isRed()                     -- 红点提醒
        isActive()                  -- 激活状态
        isOpen()                    -- 活动开启
        onLogin()                   -- 登陆回调(YunYingLogic统一调用)
        setPerpleHero()             -- 设置心仪英雄(默认是一个就不需要设置)
        onDrawCard()                -- 召换(统一从DrawCardLogic.draw调用)
        getConfigByActID()          -- 得到活动中子活动配置(统一从AbsActLogic.getLuckDraw调用)
        heroCombat()                -- 观看英雄战斗录像
--]]

function getAndSendMsg(human, id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then return end

    local actId = AbsActExcel.absActivity[id].actId
    local config = AbsActExcel.heroCome[actId]

    local msgRet = Msg.gc.GC_ABS_ACT_HERO_COME_QUERY
    msgRet.perpleHero[0] = 1
    if not absAct.perpleHero then
        absAct.perpleHero = config.heroID[1]
    end

    msgRet.perpleHero[1] = absAct.perpleHero or config.heroID[1]
    msgRet.leftCnt = config.cnt
    if absAct.noTargetHeroCnt then
        msgRet.leftCnt = config.cnt - absAct.noTargetHeroCnt
    end
    local heroConfig = config.heroID
    local len = #heroConfig
    for i = 1,len do
        HeroGrid.makeHeroSimpleByID(msgRet.heroList[i], heroConfig[i], nil, nil, nil, true)
    end
    msgRet.heroList[0] = len
    Grid.makeItem(msgRet.aItem, config.item1[1], config.item1[2])
    Grid.makeItem(msgRet.bItem, config.item2[1], config.item2[2])
    Msg.send(msgRet,human.fd)
end

function isRed(human, YYInfo, funcConfig)
    local state = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then return end

    local absConfig = AbsActExcel.absActivity[funcConfig.funcID]
    local config = AbsActExcel.heroCome[absConfig.actId]
    
    -- 能进行十连抽出现红点
    if BagLogic.getItemCnt(human, config.item2[1]) >= config.item2[2] then
        return true
    end

    return false
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

function onLogin(human, id)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if state then return end

    local config = AbsActExcel.absActivity[id]
    if not config then return end
    
    local actId = config.turns[1] -- 默认选择第一期
    local perpleHeroConfig = AbsActExcel.heroCome[actId]
    if not perpleHeroConfig then return end

    local items = { }
    local len = 0
    local have = BagLogic.getItemCnt(human, perpleHeroConfig.item1[1])
    if have > 0 and perpleHeroConfig.prop > 0 then
        len = len + 1
        items[len] = { }
        items[len][1] = ItemDefine.ITEM_JINBI_ID
        items[len][2] = have * perpleHeroConfig.prop
        BagLogic.delItem(human, perpleHeroConfig.item1[1], have, "abs_hero_come_login")
    end

    if len > 0 then
        local mailConfig = MailExcel.mail[MailDefine.MAIL_ID_ABS_HERO_COME]
        local title = mailConfig.title
        local senderName = mailConfig.senderName
        local content = mailConfig.content
        MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
    end
end

function getConfigByActID(id)
    local absActConfig = AbsActExcel.absActivity[id]
    if not absActConfig then return end

    local actId = absActConfig.actId
    return AbsActExcel.heroCome[actId]
end

function setPerpleHero(human, heroID, id)
    local state,endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then return end

    -- 英雄ID校验失败
    local config = AbsActExcel.heroCome[AbsActExcel.absActivity[id].actId].heroID
    local len = #config
    local check = false
    for i = 1,len do
        if config[i] == heroID then
            check = true
            break
        end
    end

    if not check then
        return
    end

    if absAct.perpleHero  then
        if not ObjHuman.checkRMB(human, 2000) then
	    	return
	    end

        ObjHuman.decZuanshi(human, -2000, "abs_hero_come_perple_hero")
    end

    absAct.perpleHero = heroID
end

function onDrawCardActive(human, id, op, actConfig, skip,isAct)
    local state,actID = AbsActLogic.isStartedByType(human, AbsActDefine.ABS_ACT_TYPE_19)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, actID)
    local absAct = human.db.absAct[actID]
    if not absAct then return end
    
    local config = DrawCardExcel[id]
    if not config then return end

    local heroCnt = nil
    if op == DrawCardLogic.DRAWCARD_OP_1 then
        heroCnt = 1
    elseif op == DrawCardLogic.DRAWCARD_OP_2 then
        heroCnt = 10
    else
        return
    end

    if HeroLogic.getEmptyCnt(human) < heroCnt then        
        return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
    end

    if not DrawCardLogic.checkUseItem(human, id, op, config,isAct) then
        return
    end
 
    local heroLen = 0
    local heroList = {}    
    local heroNewList = {}
    local heroIndexList = {}
    local fenjielist = {}
    local heroIDs = nil

    local randomMin = math.random(1, heroCnt)
    for i = 1, heroCnt do
        config = DrawCardExcel[id]
        if id == DrawCardLogic.DRAWCARD_ID2 and op == DrawCardLogic.DRAWCARD_OP_2 and i == randomMin then
            config = DrawCardExcel[DrawCardLogic.DRAWCARD_ID5]
        elseif id == DrawCardLogic.DRAWCARD_ID2 and op == DrawCardLogic.DRAWCARD_OP_1 then
            config = DrawCardExcel[DrawCardLogic.DRAWCARD_ID6]
        end
        
        local heroID = DrawCardLogic.randHeroID(human, id, op, config, actConfig, i == randomMin, isAct)
        local heroConfig = HeroExcel[heroID]
        local star = heroConfig.star
		local name = heroConfig.name
        local grade = heroConfig.grade
        local isNew = not HeroBook.isGet(human, heroConfig.id, heroConfig.star)
        local heroIndex, fjlist = HeroLogic.addHero(human, heroID, nil, 1, "abs_hero_come_draw_card")
        heroLen = heroLen + 1
        heroList[heroLen] = heroID
        heroNewList[heroLen] = isNew
        heroIndexList[heroLen] = heroIndex
        if fjlist and type(fjlist) == "table" then
            for fjItemID, fjItemCnt in pairs(fjlist) do
                fenjielist[fjItemID] = (fenjielist[fjItemID] or 0) + fjItemCnt
            end
        end
        if not heroIDs then
            heroIDs = heroID
        else
            heroIDs = heroIDs .. "|" .. heroID
        end

        if heroID == absAct.perpleHero and absAct.noTargetHeroCnt then
            absAct.noTargetHeroCnt = 0 
        end

        if id == DrawCardLogic.DRAWCARD_ID1 then
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE3, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID2 then
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE1, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID3 then			
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE2, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID6 then
            ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE21, grade, heroID)
        end
    end

    DrawCardLogic.sendDrawOp(human, id, op, heroList, fenjielist, heroNewList, heroIndexList, isAct)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_5012)
    Log.write(Log.LOGID_OSS_DRAWCARD, human.db._id, human.db.account, human.db.name, human.db.lv, id, heroIDs, itemID or 0, cnt or 0, zuanshi or 0, 0)

    absAct.drawCardCnt = absAct.drawCardCnt or 0
    absAct.drawCardCnt = absAct.drawCardCnt + heroCnt

    --ZhanbuLogic.addZhanbuItem(human, heroCnt * 5)
    YunYingLogic.onCallBack(human, "onDrawCard", heroCnt, nil, id)   

    -- 进行十连抽后,若道具不足十连抽,更新去掉红点
    if op == DrawCardLogic.DRAWCARD_OP_2 then
        -- 不能进行十连抽了
        local absConfig = AbsActExcel.absActivity[actID]
        local heroComeConfig = AbsActExcel.heroCome[absConfig.actId]
        if BagLogic.getItemCnt(human, heroComeConfig.item2[1]) < heroComeConfig.item2[2] then
            YunYingLogic.sendBanner(human)
            YunYingLogic.updateIcon(YYInfo[id], human)
            YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_5012)       
        end
    end
end

function heroCombat(human, heroID)
    CombatLogic.combatFightVedio(human, heroID)
end